package com.soulkun.smarthome.ui.main.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainHomeDeviceHistoryBinding
import com.soulkun.smarthome.databinding.AppFragmentMainHomeDeviceRecentBinding
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeDeviceViewModel
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeViewModel
import soulkun.library.mvvm.component.BaseDataBindingFragment

class AppMainHomeDeviceHistoryFragment(private val viewModel: AppMainHomeDeviceViewModel) :
    BaseDataBindingFragment<AppFragmentMainHomeDeviceHistoryBinding>() {

    private val tabLayoutMediator by lazy {
        TabLayoutMediator(
            requireDataBinding().tlMainHomeDeviceHistoryTabHistory,
            requireDataBinding().vp2MainHomeDeviceHistoryContainerHistory
        ){ tab, position ->
            tab.text = viewModel.historyTitleList[position]
        }
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_home_device_history
    }

    override fun initView() {
        requireDataBinding().vp2MainHomeDeviceHistoryContainerHistory.run {
            offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
            orientation = ViewPager2.ORIENTATION_HORIZONTAL
            adapter = object : FragmentStateAdapter(childFragmentManager, lifecycle){
                override fun getItemCount(): Int {
                    return viewModel.historyTitleList.size
                }

                override fun createFragment(position: Int): Fragment {
                    return if (position == 0) {
                        AppMainHomeDeviceHistoryDataFragment(viewModel)
                    }else{
                        AppMainHomeDeviceHistoryCommandFragment(viewModel)
                    }
                }
            }
            tabLayoutMediator.attach()
        }
    }

    override fun initData() {
    }

    override fun initListener() {
    }

    override fun doOnCreate() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        tabLayoutMediator.detach()
    }

}